/**
 * 
 */
package dms.presentation.action.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.DispatchAction;

import dms.dto.DormSupervisorDTO;
import dms.model.Building;
import dms.model.Campus;
import dms.model.exception.NoSuchEntityException;
import dms.model.role.DormSupervisor;
import dms.model.role.wrapper.SystemAdministratorWrapper;
import dms.model.util.PageIterator;
import dms.presentation.form.sys.DormSupervisorUpdateForm;
import dms.service.AuthenticationService;
import dms.service.BuildingService;
import dms.service.CampusService;
import dms.service.SystemAdministratorQuerySerivce;
import dms.service.imp.SystemAdministratorQueryServiceImpl;
import dms.util.Constant;
import dms.util.ContextUtil;

/**
 * @author lrz
 *
 */
public class SupervisorAction extends DispatchAction {
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		
		ActionErrors errors = new ActionErrors();
		DormSupervisorDTO dto = (DormSupervisorDTO) form;
		AuthenticationService authenticationService = (AuthenticationService) ContextUtil.getContext().getBean("authenticationService");
		
		boolean exist = authenticationService.isUserExist(dto.getName());
		if (exist) {
			errors.add("errors", new ActionMessage("该用户已存在", false));
			log.warn("用户已存在");
			this.addErrors(request, errors);
			return mapping.getInputForward();
		}
		SystemAdministratorWrapper wrapper = (SystemAdministratorWrapper) ContextUtil.getContext().getBean("systemAdministratorWrapper");
		try {
			wrapper.addDormSupervisor(dto);
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("list");
		}
		
		return mapping.findForward("list");
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		SystemAdministratorQuerySerivce systemAdministratorQuerySerivce = 
				(SystemAdministratorQueryServiceImpl) ContextUtil.getContext().getBean("systemAdministratorQuerySerivce");
		
		int start;
		int count;
		try {
			start = Integer.valueOf(request.getParameter("start"));
			count = Integer.valueOf(request.getParameter("count"));
		} catch (Exception e) {
			start = Constant.START;
			count = Constant.COUNT;
		} finally {
			
		}
				
		PageIterator<DormSupervisor> iter = systemAdministratorQuerySerivce.findAllDormSupervisor(start, count);
		request.setAttribute("page", iter);
		
		return mapping.findForward("list");
	}
	
	public ActionForward addForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		
		BuildingService buildingService = (BuildingService) ContextUtil.getContext().getBean("buildingService");
		CampusService campusService = (CampusService) ContextUtil.getContext().getBean("campusService");
		
		PageIterator<Campus> campusIter = campusService.findAll(0, 100);
		//PageIterator<Building> buildingIter = buildingService.findAll(0, 0);
		
		request.setAttribute("campus", campusIter);
		//request.setAttribute("building", buildingIter);
		
		return mapping.findForward("addForm");
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
	
		long id;
		try {
			id = Long.valueOf(request.getParameter("id"));
			
			SystemAdministratorWrapper saWrapper = (SystemAdministratorWrapper) ContextUtil.getContext().getBean("systemAdministratorWrapper");
			saWrapper.deleteDormSupervisorById(id);
		} catch (NumberFormatException e) {			
			e.printStackTrace();
			return mapping.findForward("list");
		}
		
		
		return mapping.findForward(Constant.SUCCESS);
	} 
	
	public ActionForward updateForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		long id;
		SystemAdministratorQuerySerivce systemAdministratorQuerySerivce = (SystemAdministratorQuerySerivce) ContextUtil.getContext().getBean("systemAdministratorQuerySerivce");
		DormSupervisor supervisor = null;
		try {
			id = Long.valueOf(request.getParameter("id"));
			supervisor = systemAdministratorQuerySerivce.findDormSupervisorById(id);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return mapping.findForward("list");
		}
		
		BuildingService buildingService = (BuildingService) ContextUtil.getContext().getBean("buildingService");
		CampusService campusService = (CampusService) ContextUtil.getContext().getBean("campusService");
		
		PageIterator<Campus> campusIter = campusService.findAll(0, 100);
		PageIterator<Building> buildingIter = buildingService.findAll(0, 0);
		
		request.setAttribute("campus", campusIter);	
		request.setAttribute("supervisor", supervisor);
		
		return mapping.findForward("updateForm");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		DormSupervisorDTO dormSupervisorUpdateForm = (DormSupervisorDTO) form;
		
		SystemAdministratorWrapper wrapper = (SystemAdministratorWrapper) ContextUtil.getContext().getBean("systemAdministratorWrapper");
		
		try {
			wrapper.updateDormSupervisor(dormSupervisorUpdateForm);
		} catch (NoSuchEntityException e) {
			
			e.printStackTrace();
			return mapping.findForward("list");
		}
		
		return mapping.findForward("list");
		
	}
	
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return this.list(mapping, form, request, response);
	}
}
